<?php
	class phongbandb
	{
		public function Them($MaPhongBan = "", $TenPhongBan = "")
		{
			$sql = "INSERT INTO phongban(MaPhongBan, TenPhongBan)
					VALUES('$MaPhongBan', '$TenPhongBan')";
			
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function TonTaiTrongChucVu($MaPhongBan = "")
		{
			$sql = "SELECT * FROM chucvu WHERE 1";
			if($MaPhongBan != "")
				$sql.= " AND MaPhongBan = '$MaPhongBan'";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function CapNhat($MaPhongBan = "", $TenPhongBan = "", $MaPhongBanCu = "")
		{
			$sql = "UPDATE phongban 
					SET TenPhongBan = '$TenPhongBan', MaPhongBan = '$MaPhongBan'
					WHERE MaPhongBan = '$MaPhongBanCu'";
			
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function Xoa($MaPhongBan = "")
		{
			$sql = "DELETE FROM phongban 
					WHERE MaPhongBan = '$MaPhongBan'";
			
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function XoaTatCa()
		{
			$phongbantemp = new phongbanbs();
			$danhsachtemp = $phongbantemp->LayDanhSach();
			for($j = 0; $j < ($danhsachtemp == false ? 0 : count($danhsachtemp)); $j++)
			{
				$phongbantemp->MaPhongBan = $danhsachtemp[$j]["MaPhongBan"];
		        $TonTaiChucVu = $phongbantemp->TonTaiTrongChucVu();
				if($TonTaiChucVu != false)
				{
					echo "Có chức vụ tồn tại trong phòng ban. Vui lòng kiểm tra lại !";
					
				}
				else
				{
					$phongbantemp->Xoa();

				}		
        		header("Location:index.php?do=phongban");
			}	
        }		
		
		public function LayDanhSach($MaPhongBan = "", $TenPhongBan = "")
		{
			$sql = "SELECT * 
					FROM phongban 
					WHERE 1";
			
			if($MaPhongBan != "")
				$sql .= " AND MaPhongBan = '$MaPhongBan'";
				
			if($TenPhongBan != "")
				$sql .= " AND TenPhongBan = '$TenPhongBan'";
			
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
	}
?>